import Tab_Button from "./Tab_Button";
import PlayerCtrl from "../player/PlayerCtrl";
import Mine from "./mine/Mine";
import { NetDefine } from "../../gamenet/protobufs/NetDefine";
const websocket = require("websocket");

const {ccclass, property} = cc._decorator;

@ccclass
export default class ServerFuncHome extends cc.Component {
    @property([cc.Node])
    contentList: cc.Node[] = [];

    @property([cc.Button])
    btnList: cc.Button[] = [];

    @property(Mine)
    mineView: Mine = null;

    btnComList: Tab_Button[] = [];

    onLoad(){
        for(let i = 0; i < this.btnList.length; ++i){
            let btn = this.btnList[i];
            this.btnComList[i] = btn.getComponent(Tab_Button);
        }
    }

    start(){
        this.onTabButtonClick(null, `0`);
        this.rigisterProtocol();
    }

    rigisterProtocol(){
        let service_handles = {};
        service_handles[NetDefine.ProtocolDefine.Auth] = this.onProtocolRecv.bind(this);
        websocket.register_services_handler(service_handles);
    }

    onProtocolRecv(stype, ctype, body){
        switch (ctype) {
            case NetDefine.CmdDefine.Auth.RELOGIN: {
                cc.log(`账号在其他地方登录`);
                break;
            }
            case NetDefine.CmdDefine.Auth.EDIT_UINFO: {
                this.onUserEditRecv(body);
                break;
            }
        }
    }

    onUserEditRecv(body){
        cc.log(`修改返回`, body);
        if(body.status != NetDefine.Response.OK) return;
        PlayerCtrl.data.vo.nickName = body.nick;
        PlayerCtrl.data.vo.sex = body.usex;
        this.mineView.onFlush();
    }

    onTabButtonClick(e: cc.Event, index: string){
        for(let i = 0; i < this.btnList.length; ++i){
            let isSelect = Number(index) == i;
            let btn = this.btnList[i];
            this.btnComList[i].setIsSelect(isSelect);
            btn.interactable = !isSelect;
            this.contentList[i].active = isSelect;
        }
    }

    onDisableTab(index: string){

    }

    onEnableTab(index: string){

    }
}
